using UnityEngine;

public class Sensor : MonoBehaviour
{
	[Header("Requirement")]
	public Enemy enemy;

	private void OnTriggerEnter2D(Collider2D collision)
	{
		React(collision);
	}

	private void React(Collider2D collision)
	{
		IFood component = collision.GetComponent<IFood>();
		if (component.IsDangerous(enemy))
		{
			enemy.RunAway(component);
		}
		else if (component.IsEatable(enemy) && (enemy.target == null || Vector2.Distance(component.GetPosition(), enemy.rigidbody.position) < Vector2.Distance(enemy.target.GetPosition(), enemy.rigidbody.position)))
		{
			enemy.GoEat(component);
		}
	}
}
